package aipay.mobile.activity;

import aipay.mobile.SQLite.DatabaseHandler;
import aipay.mobile.adapter.TabsPagerAdapter;
import aipay.mobile.fragment.IntroductiOne;
import aipay.mobile.model.Acount;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.transition.Visibility;
import android.util.Log;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity implements OnClickListener {
	private ViewPager viewPager;
	private TabsPagerAdapter adapter;
	private Button screen1,screen2,screen3,screen4,screen5,btboqua;
	private DatabaseHandler db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        Initilization();
        if(db.getAcountCount()>0){
        	try {
        		Acount acount = db.CheckRegister();
				Intent i =  new Intent(this,Register.class);
				i.putExtra("acount",acount);
				startActivity(i);
			} catch (Exception e) {
				// TODO: handle exception
				Intent k = new Intent(MainActivity.this,Login.class);
	    		startActivity(k);
			}
        } else {
        	adapter = new TabsPagerAdapter(getSupportFragmentManager());
            viewPager.setAdapter(adapter);
            viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

    			@Override
    			public void onPageSelected(int position) {
    				// on changing the page
    				// make respected tab selected
//    				actionBar.setSelectedNavigationItem(position);
    				Toast.makeText(getApplicationContext(), position+"",Toast.LENGTH_SHORT).show();
    			}

    			@Override
    			public void onPageScrolled(int arg0, float arg1, int arg2) {
    			}

    			@Override
    			public void onPageScrollStateChanged(int arg0) {
    			}
    		});
            screen1.setOnClickListener(this);
            screen2.setOnClickListener(this);
            screen3.setOnClickListener(this);
            screen4.setOnClickListener(this);
            screen5.setOnClickListener(this);
            btboqua.setOnClickListener(this);

        }
 }

	private void Initilization() {
		// TODO Auto-generated method stub
		viewPager = (ViewPager) findViewById(R.id.pager);
		screen1 = (Button) findViewById(R.id.screen1);
		screen2 = (Button) findViewById(R.id.screen2);
		screen3 = (Button) findViewById(R.id.screen3);
		screen4 = (Button) findViewById(R.id.screen4);
		screen5 = (Button) findViewById(R.id.screen5);
		btboqua = (Button) findViewById(R.id.btboqua);
		db = new DatabaseHandler(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
    switch (v.getId()) {
	case R.id.screen1:
		viewPager.setCurrentItem(0);
		break;
	case R.id.screen2:
		viewPager.setCurrentItem(1);
		break;
	case R.id.screen3:
		viewPager.setCurrentItem(2);
		break;
	case R.id.screen4:
		viewPager.setCurrentItem(3);
		break;
	case R.id.screen5:
		viewPager.setCurrentItem(4);
		break;
	case R.id.btboqua:
		Intent i = new Intent(MainActivity.this,Login.class);
		startActivity(i);
	default:
		break;
	}
    }
}
